// <copyright file="MenuItemService.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Cms.Services
{
    using System.Linq;
    using System.Collections.Generic;


    using CleverCMS.Cms.Data;
    using CleverCMS.Cms.Data.Entities;
    using CleverCMS.Core.Infrastructure;

    public class MenuItemService 
    {
        private readonly ICmsGenericRepository<CmsMenuType> m_menuTypes;
        private readonly IMenuItemRepository                m_menuItemRepo;
        private readonly AppCache                           m_appCache;     

        /// <summary>
        /// .ctor
        /// </summary>
        /// <param name="menuItemData"></param>
        /// <param name="menuTypes"></param>
        public MenuItemService(IMenuItemRepository                menuItemData, 
                               ICmsGenericRepository<CmsMenuType> menuTypes)
        {
            this.m_menuItemRepo = menuItemData;
            this.m_menuTypes = menuTypes;
            this.m_appCache = new AppCache();
        }
        /// <summary>
        /// Save menu item
        /// </summary>
        /// <param name="item"></param>
        public void SaveMenuItem(CmsMenuItem item)
        {
            m_menuItemRepo.SaveMenuItem(item);
            m_appCache.Remove("AdminMenus");
        }
        /// <summary>
        /// Return menu item by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CmsMenuItem LoadMenuItem(int id)
        {
            return m_menuItemRepo.LoadMenuItem(id);
        }
        /// <summary>
        /// Delete menu item
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            m_menuItemRepo.Delete(id);
            m_appCache.Remove("AdminMenus");
        }
        /// <summary>
        /// Rename menu item
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newTitle"></param>
        public void Rename(int id, string newTitle) 
        {
            m_menuItemRepo.RenameMenuItem(id, newTitle);
            m_appCache.Remove("AdminMenus");
        }
        /// <summary>
        /// Load all menu items
        /// </summary>
        /// <returns></returns>
        public IQueryable<CmsMenuItem> LoadAllMenuItems()
        {
            return m_menuItemRepo.LoadAll();
        }
        /// <summary>
        /// Load all menu items for a paged list
        /// </summary>
        /// <param name="start"></param>
        /// <param name="size"></param>
        /// <param name="sortOrder"></param>
        /// <param name="menuId"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public IQueryable<CmsMenuItem> LoadAllMenuItems(int start, int size, string sortOrder, int? menuId, out int count)
        {
            var menuItems = m_menuItemRepo.LoadAll(orderBy: q => q.OrderByDescending(d => d.Id))
                                                        .Where(x=> x.MenuId == menuId )
                                                        .Skip(start)
                                                        .Take(size);
           
            count = m_menuItemRepo.Count(menuId);

            return menuItems;
        }
        /// <summary>
        /// Load msenus
        /// </summary>
        /// <returns></returns>
        public IQueryable<CmsMenuType> LoadMenues()
        {
            return m_menuTypes.LoadAll();
        }

    }
}